package com.zrrd.jt_admin.shop.goods.controller;


import com.zrrd.jt_admin.common.bo.ExpressBo;
import com.zrrd.jt_admin.common.dto.CheckDto;
import com.zrrd.jt_admin.common.entity.Category;
import com.zrrd.jt_admin.common.entity.Goods;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.BeforeCreateVo;
import com.zrrd.jt_admin.common.page.GoodsPageVo;
import com.zrrd.jt_admin.set.express.service.ExpressService;
import com.zrrd.jt_admin.shop.category.service.CategoryService;
import com.zrrd.jt_admin.shop.goods.service.GoodsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanjl
 * @since 2024-06-29
 */
@RestController
@RequestMapping("/admin/goods")
@ResponseBody
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private CategoryService categoryService;
    @Resource
    private ExpressService expressService;

    @ApiOperation(value = "分页查询",notes = "分页查询商品列表")
    @GetMapping("/{page}")
    public ResponseResult findGoodsByPage(@PathVariable("page") Integer page,
                                          @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit,
                                          @RequestParam(value = "tab") String tab,
                                          @RequestParam(value = "title",required = false,defaultValue = "") String title,
                                          @RequestParam(value = "categoryId",required = false) Integer categoryId) {
        GoodsPageVo goodsPageVo = goodsService.findGoodsByPage(page,limit,tab,title,categoryId);
        return ResponseResult.queryResponse(goodsPageVo);
    }

    @ApiOperation(value = "创建前查询",notes = "创建商品前查询分类信息以及物流信息")
    @GetMapping("/beforeCreate")
    public ResponseResult beforeCreate() {
        List<Category> categoryList = categoryService.list();
        List<ExpressBo> expressBoList=expressService.findAll();
        BeforeCreateVo beforeCreateVo = new BeforeCreateVo();
        beforeCreateVo.setCates(categoryList);
        beforeCreateVo.setExpress(expressBoList);

        return ResponseResult.queryResponse(beforeCreateVo);
    }
    @ApiOperation(value = "发布商品",notes = "发布商品")
    @PostMapping()
    public ResponseResult addGoods(@RequestBody Goods goods) {
        boolean flag = goodsService.save(goods);
        return ResponseResult.addResponse(flag);
    }

    @ApiOperation(value = "根据id查询",notes = "根据id查询商品")
    @GetMapping("/read/{id}")
    public ResponseResult getGoodsById(@PathVariable Integer id) {

        Goods goods = goodsService.getById(id);
        return ResponseResult.queryResponse(goods);
    }

    @ApiOperation(value = "修改商品",notes = "修改商品")
    @PutMapping
    public ResponseResult updateGoods(@RequestBody Goods goods) {

        boolean flag = goodsService.updateById(goods);
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "审核",notes = "审核商品状态， 0审核中 1通过 2拒绝")
    @PostMapping("/{id}/check")
    public ResponseResult check(@PathVariable Integer id, @RequestBody CheckDto checkDto) {

        boolean flag = goodsService.lambdaUpdate()
                .eq(Goods::getId, id)
                .set(Goods::getIscheck, checkDto.getIscheck())
                .update();
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "移入回收站",notes = "删除商品，将商品放入回收站")
    @PostMapping("/{id}/delete")
    public ResponseResult delete(@PathVariable Integer id) {

        boolean flag = goodsService.lambdaUpdate()
                .eq(Goods::getId, id)
                .set(Goods::getDel, 0)
                .update();
        return ResponseResult.removeResponse(flag);
    }

    @ApiOperation(value = "彻底删除",notes = "彻底删除商品")
    @PostMapping("/{id}/destroy")
    public ResponseResult destroy(@PathVariable Integer id) {

        boolean flag = goodsService.removeById(id);
        return ResponseResult.removeResponse(flag);
    }
}

